package chat4_tedu.mychatClient.ui;

import chat4_tedu.mychatClient.Client;
import chat4_tedu.mychatClient.utils.IOUtil;
import chat4_tedu.mychatServer.vo.Request_Command;
import javax.swing.*;
import java.awt.*;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Iterator;
import java.util.Map;

public class TalkWindow {

	private JFrame jFrame = null; // @jve:decl-index=0:visual-constraint="168,56"
	private JPanel jContentPane = null;
	private JPanel jPanel = null;
	private JTextField jTextField = null;
	private JButton sendBtn = null;
	private JScrollPane jScrollPane = null;
	private JTextArea jTextAreaMessages = null;
	private Client clientMain;
//	private String login_username;
	private String sendTo_Username;//聊天的另一方(聊天的对方)
	private OutputStream out;// 连接服务器端的输出流

	public TalkWindow(String from_username, String target, Client clientMain) {
//		this.login_username = from_username;
		this.sendTo_Username = target;
		this.clientMain = clientMain;
		/*if (target.equals("ALL")) {
			jFrame.setTitle(clientMain.getMe()+",群聊中...");
		} else {
			jFrame.setTitle("与" + target + "聊天中...");
		}*/
		try {
			out = clientMain.getSocket().getOutputStream();
		} catch (IOException e) {
			e.printStackTrace();
		}
		getJFrame().setVisible(true);//当登录成功后，在ClientMain的线程里会调用此构造创建一个TalkWindow对象，这时就需要设置可见性为true

	}


	/**
	 * This method initializes jFrame
	 */
	private JFrame getJFrame() {
		if (jFrame == null) {
			jFrame = new JFrame(clientMain.getMe()+",群聊中...");
			jFrame.setLayout(null);
			jFrame.setSize(new Dimension(361, 349));
			jFrame.setResizable(false);

			if (jContentPane == null) {
				jContentPane = new JPanel();
				jContentPane.setLayout(null);


				if (jPanel == null) {
					jPanel = new JPanel();
					jPanel.setLayout(null);
					jPanel.setBounds(new Rectangle(0, 1, 345, 310));

					if (jTextField == null) {
						jTextField = new JTextField();
						jTextField.setBounds(new Rectangle(5, 203, 333, 35));
						jTextField.addKeyListener(new java.awt.event.KeyAdapter() {
							public void keyPressed(java.awt.event.KeyEvent e) {
								if (e.getKeyCode() == java.awt.event.KeyEvent.VK_ENTER) {
									sendMessageToServer();
								}
							}
						});
					}
					jPanel.add(jTextField, null);
//					jPanel.add(getJTextField(), null);


					if (sendBtn == null) {
						sendBtn = new JButton("发送");
						sendBtn.setBounds(new Rectangle(193, 254, 138, 49));
						sendBtn.addActionListener(new java.awt.event.ActionListener() {
							public void actionPerformed(java.awt.event.ActionEvent e) {
								sendMessageToServer();
							}
						});
					}
					jPanel.add(sendBtn, null);
//					jPanel.add(getJButton(), null);

					jPanel.setVisible(true);


					if (jScrollPane == null) {
						jScrollPane = new JScrollPane();
						jScrollPane.setBounds(new Rectangle(1, 1, 336, 194));


						if (jTextAreaMessages == null) {
							jTextAreaMessages = new JTextArea();
							jTextAreaMessages.setEnabled(true);
							jTextAreaMessages.setEditable(false);
						}
						jScrollPane.setViewportView(jTextAreaMessages);
//						jScrollPane.setViewportView(getJTextArea());
					}
					jPanel.add(jScrollPane, null);
//					jPanel.add(getJScrollPane(), null);
				}
				jContentPane.add(jPanel, null);
//				jContentPane.add(getJPanel(), null);
			}

			jFrame.setContentPane(jContentPane);
//			jFrame.setContentPane(getJContentPane());
			jFrame.addWindowListener(new java.awt.event.WindowAdapter() {
				public void windowClosing(java.awt.event.WindowEvent e) {
					jFrame.dispose();
					//如果当前窗口被关闭了，则从clientMain中的HashMap<String,TalkWindow>  allTalkWindow将此窗口删除
					clientMain.removeTalkWindowInClient(TalkWindow.this);
				}
			});
			jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		}
		jFrame.setVisible(true);
		jFrame.setLocation(420,200);
		return jFrame;
	}



	// 关闭窗口的同时删除聊天窗口信息
	/*public void closeFrameAndClearTalkWindowInfo() {
		for (int i = 0; i < clientMain.getAllTalkWindowInfos().size(); i++) {
			if (clientMain.getAllTalkWindowInfos().get(i).getTalkingWindow().equals(this)) {
				clientMain.getAllTalkWindowInfos().remove(i);
				break;
			}
		}
		jFrame.dispose();
	}*/


	// 关闭聊天窗口
	public void closeFrame() {
		jFrame.dispose();
	}








	public boolean sendMessageToServer() {
		// 如果已经没有好友在线，则告知用户，此好友已下线，就用return结束此方法。
		if (clientMain.getOnLineUserList().size() == 0) {
			jTextAreaMessages.setText("已无用户在线...\n");
			this.jScrollPane.getVerticalScrollBar().setValue(jScrollPane.getVerticalScrollBar().getMaximum());
			return false;
		}

		// 没有目标对象就是群聊
		if (sendTo_Username.equals("ALL")) {
			IOUtil.writeShort(Request_Command.SEND_TEXT, out);
			IOUtil.writeString(jTextField.getText(), out);
			//将输入框中的文本和上边文本区中的文本拼接到一起放入到文本区
			jTextAreaMessages.setText(jTextAreaMessages.getText() + "我说:\n   "+ jTextField.getText() + "\n");
			this.jScrollPane.getVerticalScrollBar().setValue(
					jScrollPane.getVerticalScrollBar().getMaximum());
			jTextField.setText("");
			return true;
		}


		/*if (clientMain.getFriendUserInfos().size() > 0) {
			boolean friendOnline = false;// 当前聊天对象是否在线(聊天的另一方是否在线)
			for (int i = 0; i < clientMain.getFriendUserInfos().size(); i++) {
				if (clientMain.getFriendUserInfos().get(i).equals(sendTo_Username)) {
					friendOnline = true;
					break;
				}
			}
			// 如果当前聊天对象不在线，则告知用户，此好友已下线
			if (!friendOnline) {
				//对方不在线就拼接 "用户不在线" 放到文本区中
				jTextAreaMessages.setText(jTextAreaMessages.getText() + this.sendTo_Username+ "已下线..信息发送失败\n");
				this.jScrollPane.getVerticalScrollBar().setValue(
						jScrollPane.getVerticalScrollBar().getMaximum());
				return false;
			}
		}*/

		// 如果输入框中有信息，将聊天信息发送给服务器端
		/*if (!jTextField.getText().equals("")) {
			IOUtil.writeShort(Request_Command.SEND_TEXT_TO_ONE, out);
			IOUtil.writeString(sendTo_Username, out);
			IOUtil.writeString(jTextField.getText(), out);
			jTextAreaMessages.setText(jTextAreaMessages.getText() + "我说:\n   "
					+ jTextField.getText() + "\n");
			this.jScrollPane.getVerticalScrollBar().setValue(
					jScrollPane.getVerticalScrollBar().getMaximum());
			jTextField.setText("");
			return true;
		}*/
		return false;
	}

	// 将聊天信息显示到聊天对话框中
/*	public void showMessage(String message) {
		this.jTextArea.setText(this.jTextArea.getText() + target + "说:\n   "
				+ message + "\n");
		this.jScrollPane.getVerticalScrollBar().setValue(
				jScrollPane.getVerticalScrollBar().getMaximum());
	}*/

	// 显示群聊信息
	public void showAllMessage(String from, String message) {
		this.jTextAreaMessages.setText(this.jTextAreaMessages.getText() + from + "说:\n   "
				+ message + "\n");
		this.jScrollPane.getVerticalScrollBar().setValue(
				jScrollPane.getVerticalScrollBar().getMaximum());
	}


}
